package handler

import (
	"fmt"
	"github.com/gccai/write_helper/config"
	"github.com/gccai/write_helper/enum"
	"github.com/gccai/write_helper/model"
	"github.com/gccai/write_helper/query"
	"github.com/gccai/write_helper/resp"
	"github.com/gccai/write_helper/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserHandler struct {
	UserSrv service.UserSrv
}

// 返回数据的一个格式
func (h *UserHandler) GetEntity(result model.User) resp.User {
	return resp.User{
		Id:       result.UserId,
		Key:      result.UserId,
		UserId:   result.UserId,
		NickName: result.NickName,
		Address:  result.Address,
		Mobile:   result.Mobile,
		IsDelete: result.IsDeleted,
		IsLocked: result.IsLocked,
	}
}

// 只有按照 id 进行查寻 ， 可以 补充用mobile来查寻
func (h *UserHandler) UserInfoHandler(c *gin.Context) {
	// 定义一个失败的entity实体
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Total:     0,
		TotalPage: 1,
		Data:      nil,
	}

	userId := c.Param("id")
	if userId == "" {
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	u := model.User{
		UserId: userId,
	}

	result, err := h.UserSrv.Get(u)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	r := h.GetEntity(*result)

	entity = resp.Entity{
		Code:      http.StatusOK,
		Msg:       "ok",
		Total:     0,
		TotalPage: 0,
		Data:      r,
	}
	c.JSON(http.StatusOK, gin.H{"entity": entity})
}

// 获取 整个 用户 列表
func (h *UserHandler) UserListHandler(c *gin.Context) {
	var q query.ListQuery
	entity := resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Total:     0,
		TotalPage: 1,
		Data:      nil,
	}
	// get 查寻
	err := c.ShouldBindQuery(&q)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
	}
	// fmt.Println(q)
	list, err := h.UserSrv.List(&q)
	if err != nil {
		panic(err)
	}
	total, err := h.UserSrv.GetTotal(&q)
	if err != nil {
		panic(err)
	}

	if q.PageSize < 0 {
		q.PageSize = config.PAGE_SIZE
	}
	ret := int(total % q.PageSize)
	ret2 := int(total / q.PageSize)
	totalPage := 0
	//fmt.Println(totalPage)
	if ret == 0 {
		totalPage = ret2
	} else {
		totalPage = ret2 + 1
	}

	var newList []*resp.User
	for _, item := range list {
		r := h.GetEntity(*item)
		newList = append(newList, &r)
	}

	entity = resp.Entity{
		Code:      http.StatusOK,
		Msg:       "OK",
		Total:     total,
		TotalPage: totalPage,
		Data:      newList,
	}
	c.JSON(http.StatusOK, gin.H{"entity": entity})
}

// 添加 用户
func (h *UserHandler) AddUserHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:  int(enum.OperateFail),
		Msg:   enum.OperateFail.String(),
		Total: 0,
		Data:  nil,
	}
	u := model.User{}
	err := c.ShouldBindJSON(&u)
	if err != nil {
		fmt.Println("绑定user出错")
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	// 检查 操作是否出现问题
	r, err := h.UserSrv.Add(u)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	if r.UserId == "" {
		entity.Msg = fmt.Sprint("userid 消失")
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	// 检查无误后操作
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{"entity": entity})
}

// 更改用户信息
func (h *UserHandler) EditUserHandler(c *gin.Context) {
	u := model.User{}
	entity := resp.Entity{
		Code:  int(enum.OperateFail),
		Msg:   enum.OperateFail.String(),
		Data:  nil,
		Total: 0,
	}

	err := c.ShouldBindJSON(&u)
	//fmt.Println(u)
	if err != nil {
		entity.Msg = "绑定参数的时候出现问题"
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}

	b, err := h.UserSrv.Edit(u)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
	}
	if b {
		entity.Code = int(enum.OperateOk)
		entity.Msg = enum.OperateOk.String()
		c.JSON(http.StatusOK, gin.H{"entity": entity})
	}
}

// 删除用户信息
func (h *UserHandler) DeleteUserHandler(c *gin.Context) {
	entity := resp.Entity{
		Code: int(enum.OperateFail),
		Msg:  enum.OperateFail.String(),
		Data: nil,
	}
	id := c.Param("id")
	b, err := h.UserSrv.Delete(id)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusInternalServerError, gin.H{"entity": entity})
		return
	}
	if b {
		entity.Code = int(enum.OperateOk)
		entity.Msg = enum.OperateOk.String()
		c.JSON(http.StatusOK, gin.H{"entity": entity})
	}
}
